﻿
using System.ComponentModel;
using demo.Web.Services;
using System.ServiceModel.DomainServices.Client;
using demo.Web;
using System.Collections.ObjectModel;

using demo.Web.PresentationModel;

namespace demo.ViewModels
{
    public class HomeVM : INotifyPropertyChanged
    {

        DeptDomainContext ctx = new DeptDomainContext();


        private bool _eagerLoad;
        public bool EagerLoad
        {
            get { return _eagerLoad; }
            set
            {
                _eagerLoad = value;
                NotifyPropertyChanged("EagerLoad");
            }
        }


        private ObservableCollection<PMDepartment> _departments;
        public ObservableCollection<PMDepartment> Departments
        {
            get { return _departments; }
            set
            {
                _departments = value;
                NotifyPropertyChanged("Departments");
            }
        }


        private PMDepartment _selectedDepartment;
        public PMDepartment SelectedDepartment
        {
            get { return _selectedDepartment; }
            set
            {
                _selectedDepartment = value;
                NotifyPropertyChanged("SelectedDepartment");
            }
        }



        public void LoadDepts()
        {
            // start clean each time we reload or change the loading mode
            ctx.PMDepartments.Clear();
            ctx.PMEmployees.Clear();
            this.Departments = null;
            this.SelectedDepartment = null;

            var qry = ctx.GetDepartmentsPMQuery(EagerLoad);
            ctx.Load<PMDepartment>(qry, delegate(LoadOperation<PMDepartment> lop)
            {
                ObservableCollection<PMDepartment> depts = new ObservableCollection<PMDepartment>();
                foreach (PMDepartment d in lop.Entities)
                {
                    depts.Add(d);
                }
                this.Departments = depts;
            }
            , null);
        }


        public void LoadEmployeesForDept(string deptID)
        {
            if (EagerLoad)
                return;                     // don't do this for eager loading

            var qry = ctx.GetPMEmployeesForDeptQuery(deptID);
            ctx.Load<PMEmployee>(qry, delegate(LoadOperation<PMEmployee> lop)
            {
                NotifyPropertyChanged("SelectedDepartment");
            }
            , null);
        }


     

        public void AddDept(string name)
        {
            PMDepartment d = new PMDepartment() { Name = name };
            ctx.PMDepartments.Add(d);
            ctx.SubmitChanges(delegate(SubmitOperation so)
            {
                if (!so.HasError)
                {
                    this.Departments.Add(d);
                }

            }, null);


        }


        public void AddEmp(string deptId, string name, string addr, string ext)
        {

            PMEmployee emp = new PMEmployee() { Name = name, Addr = addr, Ext = ext, DeptID = deptId };
            ctx.PMEmployees.Add(emp);
            ctx.SubmitChanges(delegate(SubmitOperation so)
            {
                if (!so.HasError)
                {
                    NotifyPropertyChanged("SelectedDepartment");
                }

            }, null);

        }


        public void SaveChanges()
        {
            ctx.SubmitChanges();
        }



        public event PropertyChangedEventHandler PropertyChanged;

        protected void NotifyPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}
